package com.newhero.security;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import org.bouncycastle.util.encoders.Base64;

import com.newhero.product.common.util.GTime;

/**
*
* 创建时间 2017-7-5
*
* <li>Project: BasePro
* <li>Packgae:com.newhero.security
* <li>File Name: LicenseUtil.java
* <li>Description:
* <li>Copyright: Copyright (c) 2009-2012 Newhero Co. Ltd.
* <li>Company: 河北新禾科技有限公司
*
* @author 王士江
* @version 1.0
* $Id: LicenseUtil.java,v 1.0 2017-7-5 上午11:08:45 wsjjiang Exp $
*/

public class LicenseUtil  implements Serializable
{
    private static final long serialVersionUID = 2425971237592505266L;
    private static LicenseUtil rsa = new LicenseUtil();
    @SuppressWarnings("unused")
	private static byte[] license;
    
    private LicenseUtil()
    {
        
    }
    
    public static LicenseUtil getInstance()
    {
        return rsa;
    }

    public static boolean checkSystemLicense()
    {
    	if("NEWHERO".equals(getSystemLicense()))
    	{
    		return true;
    	}
    	return false;
    }
    /**
     * 检查许可证权限信息
     * @return
     */
	public static boolean checkBaseLicense()
	{
    	try 
        {
        	String license = new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[1];
        	if(checkSystemLicense())
        	{
        		if(NewheroLicense.getInstance().macAddrIsNotNull() && license.indexOf(NewheroLicense.getInstance().getThisMacAddr())!=-1)
        		{
        			//System.out.println("系统为试用版，注册码为："+new String(Base64.encode(NewheroLicense.getInstance().getThisMacAddr().getBytes()))+"|"+NewheroLicense.getInstance().getThisMacAddr().replaceAll("-", ""));
        			return true;
        		}
        	}
        	else
        	{
        		if(NewheroLicense.getInstance().macAddrIsNotNull() && license.indexOf(NewheroLicense.getInstance().getThisMacAddr())!=-1)
        		{
        			System.out.println("系统为试用版，注册码为："+new String(Base64.encode(NewheroLicense.getInstance().getThisMacAddr().getBytes()))+"|"+NewheroLicense.getInstance().getThisMacAddr().replaceAll("-", ""));
        			System.out.println("试用到期时间："+new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[2]);
        			return checkDateLicense();
        		}
        		else if("TESTDEV".equals(getSystemLicense()))
        		{
        			System.out.println("系统为试用版，注册码为："+new String(Base64.encode(NewheroLicense.getInstance().getThisMacAddr().getBytes()))+"|"+NewheroLicense.getInstance().getThisMacAddr().replaceAll("-", ""));
        			System.out.println("试用到期时间："+new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[2]);
        			return checkDateLicense();
        		}
        	}
		} 
        catch (Exception e) 
        {
			e.printStackTrace();
		}
        System.out.println("系统为试用版，注册码为："+new String(Base64.encode(NewheroLicense.getInstance().getThisMacAddr().getBytes()))+"|"+NewheroLicense.getInstance().getThisMacAddr().replaceAll("-", ""));
        System.out.println("试用到期时间："+new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[2]);
        return false;
    }
    
    public static boolean checkLicense()
    {
    	if("NEWHERO".equals(getSystemLicense()))
    	{
    		return checkBaseLicense();
    	}
    	else
    	{
    		if(checkBaseLicense())
    		{
    			return true;
    		}
    		else
    		{
    			return checkDateLicense();
    		}
    	}
    }
    
    public static int getSystemModel()
    {
    	return NewheroLicense.getInstance().getThisModel();
    }
    
    /*
    public static boolean checkBaseLicense()
    {
        try 
        {
        	String license = new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[1];
        	if(NewheroLicense.getInstance().macAddrIsNotNull() && license.indexOf(NewheroLicense.getInstance().getThisMacAddr())!=-1)
        	{
        		return true;
        	}
		} 
        catch (Exception e) 
        {
			e.printStackTrace();
		}
        return false;
    }
    */
    public static boolean checkDateLicense()
    {
    	try 
    	{
    		String license = new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[2];
    		if(Integer.parseInt(GTime.getTime(GTime.YYYYMMDD))<=Integer.parseInt(license))
    		{
    			return true;
    		}
		} 
    	catch (Exception e) 
    	{
			e.printStackTrace();
		}
    	return false;
    }
    
    public static int getUserLicense()
    {
    	if(!checkLicense())
    	{
    		return -1;
    	}
    	try 
    	{
    		return Integer.parseInt(new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[4]);
		} 
    	catch (Exception e) 
    	{
			e.printStackTrace();
		}
    	return 5;
    }
    
    public static int getDeptLicense()
    {
    	if(!checkLicense())
    	{
    		return -1;
    	}
    	try 
    	{
    		return Integer.parseInt(new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[3]);
		} 
    	catch (Exception e) 
    	{
			e.printStackTrace();
			return 5;
		}
    }
    
    public static String getSystemLicense()
    {
    	try 
    	{
    		return new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[0];
    	} 
    	catch (Exception e) 
    	{
    		e.printStackTrace();
    		return "TEST";
    	}
    }
    
    private static byte[] getLicense()
    {
        InputStream in = LicenseUtil.class.getClassLoader().getResourceAsStream("license.newhero");
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] tmpbuf = new byte[1024];
        int count = 0;
        try
        {
            while ((count = in.read(tmpbuf)) != -1) 
            {
                bout.write(tmpbuf, 0, count);
                tmpbuf = new byte[1024];
            }
        } 
        catch (IOException e)
        {
            
        }
        finally
        {
            try
            {
                in.close();
            } 
            catch (IOException e)
            {
            }
        }
        return bout.toByteArray();
    }
    
    static
    {
    	NewheroLicense.getInstance().initLicense(getLicense());
    	try 
    	{
    		NewheroLicense.getInstance().setThisModel(Integer.parseInt(new String(PubRSAUtil.decrypt(NewheroLicense.getInstance().getThisLicense())).split(";")[5]));
		} 
    	catch (Exception e) 
    	{
			e.printStackTrace();
		}
    }
    
    public static void main(String[] args) throws IOException
    {
    	System.out.println(LicenseUtil.checkLicense());
        System.out.println(LicenseUtil.getSystemLicense());
        String licenseDate = new String(com.newhero.security.PubRSAUtil.decrypt(com.newhero.security.NewheroLicense.getInstance().getThisLicense())).split(";")[2];
        System.out.println(licenseDate);
//        System.out.println(LicenseUtil.checkBaseLicense());
//        System.out.println(LicenseUtil.checkDateLicense());
//        System.out.println(LicenseUtil.getDeptLicense());
//        System.out.println(LicenseUtil.getUserLicense());
//        System.out.println(LicenseUtil.getSystemModel());
    }
}